package homework.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Map;

public class Server {
    private ServerSocket serverSocket;
    private Map<String, PrintWriter> map;
    public Server (){
        try {
            serverSocket = new ServerSocket(9991);
            } catch (IOException e) {
            e.printStackTrace();
            }
        }
    void start(){
        while (true) {
            try {
                Socket socket = serverSocket.accept();
                ClientHandler clientHandler = new ClientHandler(socket);
                Thread thread = new Thread(clientHandler);
                thread.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
private class ClientHandler implements Runnable{
        private Socket socket;
        private String nickName;
        private String IP;
        public ClientHandler (Socket socket) {
            this.socket = socket;
            IP = socket.getInetAddress().getHostAddress();
        }
        @Override
        public void run() {
            PrintWriter pw = null;
            try {
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
                System.out.println(1111111);

                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os,StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw,true);
                System.out.println("222");
                nickName = br.readLine();
                System.out.println(nickName);
                synchronized (map) {
                    map.put(nickName,pw);
                }
                sendMessageToAll("上线了，当前人数"+map.size());
                String line;
                while ((line=br.readLine())!=null){
                    if (line.startsWith("@")){
                        sendMessageToOne(line.substring(line.indexOf(":"),line.length()));
                    }else {
                        sendMessageToAll(line);
                    }
                }
            } catch (IOException e) {
            } finally {
                try {
                    synchronized (map) {
                        map.remove(pw);
                    }
                    sendMessageToAll("下线了，当前人数"+map.size());
                    socket.close();
                } catch (IOException e) {
                }
            }
        }
        void sendMessageToAll(String message){
            synchronized (map) {
                map.forEach((k,v)->v.println(k+"["+IP+"]："+message));
            }
        }
        void sendMessageToOne(String message){
            String nickName = message.substring(1,message.indexOf(":"));
            map.get(nickName).println(message);
        }
    }
    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
}
